package com.trio.gamerental.controller;

import com.trio.gamerental.pageModel.SendEMaileHelper;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
 * Created by 张俊强~ on 2016/7/30.
 */
@Controller
@RequestMapping("/email")
public class SendEMailController {


    // 发送邮件的服务器的IP和端口
    private String mailServerHost;
    private String mailServerPort = "25";
    // 邮件发送者的地址
    private String fromAddress;
    // 登陆邮件发送服务器的用户名和密码
    private String userName;
    private String password;
    // 是否需要身份验证
    private boolean validate = false;
    // 邮件主题
    private String subject;
    // 邮件的文本内容
    private String content;
    // 邮件接收者的地址
    private String[] toAddress;


    @ResponseBody
    @RequestMapping(value = "/sendMail", method = RequestMethod.POST)
    public SendEMaileHelper sendMail(String email){
   //     Map<String,Integer> map=new HashMap<String,Integer>();
        String random= RandomStringUtils.random(6,new char[]{'1','2','3','4','5','6', '7', '8', '9','0'});

        SendEMaileHelper eMaileHelper=new SendEMaileHelper();
       if(this.send(random, email, "图书漂流App验证码")){
           eMaileHelper.setMessage("获取成功");
           eMaileHelper.setSuccess(true);
           eMaileHelper.setCode(random);
         //  return random;
       }
       else{
           eMaileHelper.setMessage("获取失败");
           eMaileHelper.setSuccess(false);
       }
        return eMaileHelper;
    }


    public String[] getToAddress() {
        return toAddress;
    }

    public void setToAddress(String[] toAddress) {
        this.toAddress = toAddress;
    }

    public String getMailServerHost() {
        return mailServerHost;
    }

    public void setMailServerHost(String mailServerHost) {
        this.mailServerHost = mailServerHost;
    }

    public String getMailServerPort() {
        return mailServerPort;
    }

    public void setMailServerPort(String mailServerPort) {
        this.mailServerPort = mailServerPort;
    }

    public boolean isValidate() {
        return validate;
    }

    public void setValidate(boolean validate) {
        this.validate = validate;
    }

    public String getFromAddress() {
        return fromAddress;
    }

    public void setFromAddress(String fromAddress) {
        this.fromAddress = fromAddress;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String textContent) {
        this.content = textContent;
    }

    public boolean send(String txt, String to, String subject) {

        this.setMailServerHost("smtp.163.com");
        this.setMailServerPort("25");
        this.setValidate(true);
        this.setUserName("13110639520@163.com");
        this.setPassword("zjq1314520zjq");
        this.setFromAddress("13110639520@163.com");
        this.setToAddress(new String[]{to});
        this.setSubject(subject);
        this.setContent(txt);

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.put("mail.smtp.port",this.getMailServerPort());
        props.put("mail.smtp.host",this.getMailServerHost());
        props.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);
// 用session为参数定义消息对象
        MimeMessage message = new MimeMessage(session);
        try {
// 加载发件人地址
            message.setFrom(new InternetAddress(this.getFromAddress()));
// 加载收件人地址
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            Multipart multipart = new MimeMultipart();
            BodyPart contentPart = new MimeBodyPart();

            contentPart.setContent(this.getContent(), "text/html; charset=utf-8");


            multipart.addBodyPart(contentPart);
            message.setContent(multipart);
// 保存邮件
            message.saveChanges();
// 发送邮件
            System.out.println("============="+this.mailServerHost+"=="+this.userName+"==="+this.password);

            Transport transport = session.getTransport("smtp");
// 连接服务器的邮箱
            transport.connect(this.mailServerHost,this.fromAddress,this.password);
// 把邮件发送出去
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
